package com.zzw.bridge.business.controller;

import com.zzw.bridge.business.dto.FileDownloadDTO;
import com.zzw.bridge.business.service.FileService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;


/**
 * 文件相关接口
 *
 * @author XiaoJie
 * @date 2025/5/22 13:34
 */
@Tag(name = "文件相关接口控制器")
@RestController
@RequestMapping("business/file")
@Slf4j
public class FileController {

    @Autowired
    private FileService fileService;

    @Operation(summary = "文件下载")
    @PostMapping("/download")
    public void downloadFile(@RequestBody @Validated FileDownloadDTO dto, HttpServletResponse response) {
       fileService.downloadFile(dto, response);
    }

    @GetMapping("/getDownload/{filePath}/{fileName}")
    @Operation(summary = "ResponseEntity<Resource>文件下载")
    public ResponseEntity<Resource> downloadFileResponseEntity(@PathVariable("filePath") String filePath,@PathVariable("fileName") String fileName) throws UnsupportedEncodingException {
        FileDownloadDTO dto = new FileDownloadDTO(filePath, fileName);
        return fileService.downloadFileResponseEntity(dto);
    }
}
